package com.yfq.springframework.core.io;

import com.yfq.springframework.util.ClassUtils;

import java.io.InputStream;

/**
 * @author 姚凤桥
 * @date 2023/1/19 12:04
 * @description
 */
public class ClassPathResource implements Resource {
    private final String classpath;

    private final ClassLoader classLoader;

    public ClassPathResource(String classpath) {
        this.classpath = classpath;
        this.classLoader = ClassUtils.getDefaultClassLoader();
    }

    public ClassPathResource(String classpath, ClassLoader classLoader) {
        this.classpath = classpath;
        this.classLoader = classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader();
    }

    @Override
    public InputStream getInputStream() {
        return classLoader.getResourceAsStream(classpath);
    }
}
